package com.chao.yyks.modular.ranking.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chao.common.util.RequestUtile;
import com.chao.common.util.StrUtile;
import com.chao.common.util.exception.SysException;
import com.chao.yyks.modular.ranking.bean.EnRankingLabel;
import com.chao.yyks.modular.ranking.mapper.EnRankingLabelMapper;
import com.chao.yyks.modular.ranking.service.EnRankingLabelService;
import com.chao.yyks.modular.sys.bean.EnStudent;
import com.chao.yyks.modular.sys.mapper.EnStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 学生板块排名ServiceImpl
 * chao
 * 2024-09-15
 */
@Service
public class EnRankingLabelServiceImpl implements EnRankingLabelService {

	/**
	 * 学生板块排名Mapper
	 */
	@Autowired
	private EnRankingLabelMapper enRankingLabelMapper;

	/**
	 * 学生Mapper
	 */
	@Autowired
	private EnStudentMapper enStudentMapper;

	/**
	 * 新增学生板块排名
	 */
	@Override
	@Transactional
	public void insertEnRankingLabel(EnRankingLabel enRankingLabel){
		if(StrUtile.isEmpty(enRankingLabel.getLabelId())){
			throw new SysException("请指定板块!");
		}
		if(null == enRankingLabel.getStudentLength()){
			enRankingLabel.setStudentLength(1);
		}
		String studentId = RequestUtile.getLoginUserId();
		enRankingLabel.setStudentId(studentId);
		EnRankingLabel enRankingLabelPojo = enRankingLabelMapper.selectEnRankingLabelByStudentId(studentId,enRankingLabel.getLabelId());
		if(null == enRankingLabelPojo){
			EnStudent enStudent = enStudentMapper.selectEnStudentByStudentId(studentId);
			if(null == enStudent){
				throw new SysException("登录账号异常,请重新登录或联系老师!");
			}
			enRankingLabel.setClasssId(enStudent.getClasssId());
			enRankingLabelMapper.insertEnRankingLabel(enRankingLabel);
			return;
		}
		enRankingLabel.setStudentLength(enRankingLabelPojo.getStudentLength()+enRankingLabel.getStudentLength());
		enRankingLabelMapper.updateEnRankingLabel(enRankingLabel);
	}

	/**
	 * 查询阅读篇数学生排名
	 */
	@Override
	public List<EnRankingLabel> selectEnRankingLength(JSONObject json){
		if(StrUtile.isEmpty(json.getString("labelId"))){
			throw new SysException("请指定板块查询!");
		}
		return enRankingLabelMapper.selectEnRankingLength(json);
	}

}